<?php

class MStudResult extends CI_Model{

	public function __construct()
	    {
	        parent::__construct();
	    }
	
	
	function getStudResult($id){
		$data = array();
	    $this->db->select('student_result.*, shift.title');
	    $this->db->from('student_result');
	    $this->db->join('shift', 'student_result.shift_id = shift.shift_id');
	    $this->db->where('student_result_id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get();
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
	
	function getStudResultPath($path){
	    $data = array();
	    $this->db->where('path',db_clean($path));
	    $this->db->where('activate', 1);
	    $this->db->limit(1);
	    $Q = $this->db->get('student_result');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
		
	 function getAllStudResults(){
	     $data = array();
	     $this->db->select('student_result.*, shift.title');
	     $this->db->from('student_result');
	     $this->db->join('shift', 'student_result.shift_id = shift.shift_id');
	     $this->db->order_by("student_result.date_created", "desc");
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
	
	
		
	 function addStudResult(){
		$data = array( 
			'result_type' => db_clean($_POST['result_type']),
			'semester' => db_clean($_POST['semester']),
			'shift_id' => db_clean($_POST['shift_id']),
			'group' => $_POST['group'],
			'batch' => db_clean($_POST['batch']),
			'year' => db_clean($_POST['year']),
			'uid' => db_clean($_SESSION['uid']),
			'activate' => db_clean($_POST['activate'])
		);
		 if ($_FILES){
			$config['upload_path'] = './images/studresult/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size'] = '2048';
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
		
			if (strlen($_FILES['result_src']['name'])){
				if(!$this->upload->do_upload('result_src')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
			
				if ($image['file_name']){
					$data['result_src'] = $image['file_name'];
				}
			}
		 }
		
		$this->db->set('date_created', 'NOW()', FALSE);
		$this->db->insert('student_result', $data);	 
	 }
	 
	 function updateStudResult(){
		$data = array( 
			'result_type' => db_clean($_POST['result_type']),
			'semester' => db_clean($_POST['semester']),
			'shift_id' => db_clean($_POST['shift_id']),
			'group' => $_POST['group'],
			'batch' => db_clean($_POST['batch']),
			'year' => db_clean($_POST['year']),
			'uid' => db_clean($_SESSION['uid']),
			'activate' => db_clean($_POST['activate'])
		);
		 if ($_FILES['result_src']['name'] != null || $_FILES['result_src']['name'] != "" ){
		 	$source = $this->db->where('student_result_id', id_clean($_POST['student_result_id']))->get('student_result')->row();
    		if (unlink('images/studresult/'.$source->result_src)) {
    			
    		}else {
    			$this->session->set_flashdata('error', 'can not delete image from directory.');
    		}
			$config['upload_path'] = './images/studresult/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size'] = '2048';
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
		
			if (strlen($_FILES['result_src']['name'])){
				if(!$this->upload->do_upload('result_src')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
			
				if ($image['file_name']){
					$data['result_src'] = $image['file_name'];
				}
			}
		 }
	 	$this->db->where('student_result_id', id_clean($_POST['student_result_id']));
	 	$this->db->set('date_modified', 'NOW()', FALSE);
		$this->db->update('student_result', $data);	
	 
	 }
	 
	 
	 function inactiveStudResult($id){
	 	$data = array('activate' => 0);
	 	$this->db->where('student_result_id', id_clean($id));
		$this->db->update('student_result', $data);	
	 }
	 
	 function deleteStudResult($id){
	 	$source = $this->db->where('student_result_id', id_clean($id))->get('student_result')->row();
	 	
	 	if($source->result_src != null){
		 	if (file_exists(FCPATH.'images/studresult/'.$source->result_src)) {
	    		unlink('images/studresult/'.$source->result_src);
	    	}else {
	    		$this->session->set_flashdata('error', 'can not delete image from directory.');
	    	}
	 	}	 	
	 	$this->db->where('student_result_id', id_clean($id));
		$this->db->delete('student_result');	
	 }
	 
	
}

?>